package com.lanou.controller;

import com.lanou.bean.Blog;
import com.lanou.bean.User;
import com.lanou.service.BlogService;
import com.lanou.util.AjaxResultUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * Created by dllo on 18/2/27.
 */
@Controller
@RequestMapping(value = "/blog")
public class BlogController {

    @Resource
    private BlogService blogService;

    private AjaxResultUtil ajaxResultUtil = new AjaxResultUtil();

    //查询所有博客
    @ResponseBody
    @RequestMapping(value = "/allblog")
    public AjaxResultUtil searchAllBlog() {
        List<Blog> list= blogService.searchAllBlog();
        ajaxResultUtil.setResultCode(1);
        ajaxResultUtil.setMessage("获取成功");
        ajaxResultUtil.setData(list);
        return ajaxResultUtil;
    }

    //添加博客
    @ResponseBody
    @RequestMapping(value = "/addblog", method = RequestMethod.POST)
    public boolean addBlog(@RequestParam(value = "title") String title,
                           @RequestParam(value = "desc") String desc,
                           @RequestParam(value = "content") String content,
                           HttpSession session) {
        User user = (User) session.getAttribute("user");
        int userId = 2;
        //插入数据库
        blogService.addBlog(title, desc, content, userId);
        return true;
    }

    //删除博客
    @RequestMapping(value = "/deleteblog")
    public void deleteBlog(@RequestParam(value = "bid") int bid) {
        blogService.deleteBlog(bid);
    }

    //查看详情
    @ResponseBody
    @RequestMapping(value = "/seemore")
    public boolean seeMore(@RequestParam(value = "bid") int bid,
                        HttpSession session) {
        Blog blog = blogService.seeMore(bid);
        session.setAttribute("blogdesc", blog);
        return blog != null;
    }
    @ResponseBody
    @RequestMapping(value = "/getmore")
    public AjaxResultUtil getMore(HttpSession session){
        Blog blog = (Blog) session.getAttribute("blogdesc");
        ajaxResultUtil.setResultCode(1);
        ajaxResultUtil.setMessage("获取博客成功");
        ajaxResultUtil.setData(blog);
        return ajaxResultUtil;
    }

    //分页
    @ResponseBody
    @RequestMapping(value = "/page")
    public int page(){
        return blogService.page();
    }
    @ResponseBody
    @RequestMapping(value = "/clickpage")
    public AjaxResultUtil clickPage(@RequestParam(value = "page") int page){
        List<Blog> list = blogService.clickPage(page);
        ajaxResultUtil.setResultCode(1);
        ajaxResultUtil.setMessage("获取列表成功");
        ajaxResultUtil.setData(list);
        return ajaxResultUtil;
    }

    //条件查询博客
    @ResponseBody
    @RequestMapping(value = "/searchbykeyword")
    public AjaxResultUtil searchByKeyWord(@RequestParam(value = "keyword") String keyWord){
        List<Blog> list =blogService.searchByKeyWord(keyWord);
        ajaxResultUtil.setResultCode(1);
        ajaxResultUtil.setMessage("获取列表成功");
        ajaxResultUtil.setData(list);
        return ajaxResultUtil;
    }

}
